package com.lcxw.hxzbadmin.configcontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbadmin.entity.requestbean.RequestBodyBasic;
import com.lcxw.hxzbadmin.entity.requestbean.SetCubeConfigReq;
import com.lcxw.hxzbadmin.entity.requestbean.SetLiveTypeReq;
import com.lcxw.hxzbadmin.entity.responseBean.GetCubeConfigResponse;
import com.lcxw.hxzbapi.service.ConfigService;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.configbean.CacheConfig;
import com.lcxw.hxzbadmin.entity.configbean.LiveConfig;
import com.lcxw.hxzbadmin.entity.configbean.RegisterConfig;
import com.lcxw.hxzbadmin.entity.configbean.SEOConfig;
import com.lcxw.hxzbadmin.entity.configbean.ShareConfig;
import com.lcxw.hxzbadmin.entity.configbean.YwpdBaseConfig;
import com.lcxw.hxzbadmin.entity.requestbean.BaseConfigBean;
import com.lcxw.hxzbadmin.service.configservice.BaseConfigService;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.RedisUtils;

@RestController
@RequestMapping("/admin/config")
@CrossOrigin
public class BaseConfigController {

    private Logger logger = LoggerFactory.getLogger(BaseConfigController.class);

    @Resource
    BaseConfigService baseConfigService;

    @Resource
    RedisUtils redisUtils;

    @Resource
    ConfigService configService;

    //设置基础配置
    @PostMapping("/base/set")
    public Response setBaseConfig(@RequestBody BaseConfigBean baseConfigParams) {
        YwpdBaseConfig ywpdBaseConfig = new YwpdBaseConfig();

        ywpdBaseConfig.setCloseSite(baseConfigParams.getCloseSite());
        ywpdBaseConfig.setCloseTip(baseConfigParams.getCloseTip());
        ywpdBaseConfig.setCopyright(baseConfigParams.getCopyright());
        ywpdBaseConfig.setLogo(baseConfigParams.getLogo());
        ywpdBaseConfig.setSiteDomain(baseConfigParams.getSiteDomain());
        ywpdBaseConfig.setSiteName(baseConfigParams.getSiteName());
        ywpdBaseConfig.setSiteTitle(baseConfigParams.getSiteTitle());
        ywpdBaseConfig.setSiteUrl(baseConfigParams.getSiteUrl());

        try {
            baseConfigService.updateBaseConfigByPrimaryKey(ywpdBaseConfig);
        } catch (Exception e) {
            return Response.dbFail(logger, "update base config");
        }

        return Response
            .common(logger, "update base config success.", 0, "success", null);


    }

    //获取基础设置
    @PostMapping("/base/get")
    public Response getBaseConfig(@RequestBody JSONObject requestParams) {

        YwpdBaseConfig ywpdBaseConfig;
        try {
            ywpdBaseConfig = baseConfigService.selectBaseConfigByPrimaryKey();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "select base config");
        }

        return Response.ok(ywpdBaseConfig);


    }

    //设置SEO相关配置
    @PostMapping("/SEO/set")
    public Response setSEOConfig(@RequestBody JSONObject requestParams) {
        SEOConfig seoConfig = new SEOConfig();

        seoConfig.setTitleSuffix(requestParams.getString("titleSuffix"));
        seoConfig.setMETA(requestParams.getString("META"));
        seoConfig.setMETAdesc(requestParams.getString("METAdesc"));

        try {
            baseConfigService.updateSEOConfigByPrimaryKey(seoConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "set seo config");
        }
        return Response.ok();
    }

    //获取SEO相关配置
    @PostMapping("/SEO/get")
    public Response getSEOConfig(@RequestBody JSONObject requestParams) {
        SEOConfig seoConfig;

        try {
            seoConfig = baseConfigService.selectSEOConfigByPrimaryKey();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "get seo config");
        }

        return Response.ok(seoConfig);

    }

    //设置注册相关
    @PostMapping("/register/set")
    public Response setRegisterConfig(@RequestBody JSONObject requestParams) {
        RegisterConfig registerConfig = new RegisterConfig();

        registerConfig.setAllowReg(requestParams.getInteger("allowReg"));
        registerConfig.setNeedCheck(requestParams.getInteger("needCheck"));
        registerConfig.setForbidden(requestParams.getString("forbidden"));
        registerConfig.setUcUrl(requestParams.getString("ucUrl"));

        try {
            baseConfigService.updateRegisterConfigByPrimaryKey(registerConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "set register config");
        }
        return Response.ok();
    }

    //查询注册相关配置
    @PostMapping("/register/get")
    public Response getRegisterConfig(@RequestBody JSONObject requestParams) {
        RegisterConfig registerConfig;

        try {
            registerConfig = baseConfigService.selectRegisterConfigByPrimaryKey();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "get seo config");
        }

        return Response.ok(registerConfig);

    }

    //设置直播相关配置
    @PostMapping("/live/set")
    public Response setLiveConfig(@RequestBody JSONObject requestParams) {
        LiveConfig liveConfig = new LiveConfig();

        liveConfig.setLiveAccess(requestParams.getInteger("liveAccess"));
        liveConfig.setRatio(requestParams.getInteger("ratio"));
        liveConfig.setCdn(requestParams.getString("cdn"));
        liveConfig.setCdnPullUrl(requestParams.getString("cdnPullUrl"));
        liveConfig.setFps(requestParams.getInteger("fps"));
        liveConfig.setMaxBit(requestParams.getInteger("maxBit"));
        liveConfig.setQuality(requestParams.getInteger("quality"));
        liveConfig.setFrame(requestParams.getInteger("frame"));
        liveConfig.setResolutionWidth(requestParams.getInteger("resolutionWidth"));
        liveConfig.setResolutionHeight(requestParams.getInteger("resolutionHeight"));

        try {
            baseConfigService.updateLiveConfigByPrimaryKey(liveConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "set live config");
        }
        return Response.ok();
    }

    //查询直播相关配置
    @PostMapping("/live/get")
    public Response getLiveConfig(@RequestBody JSONObject requestParams) {
        LiveConfig liveConfig;

        try {
            liveConfig = baseConfigService.selectLiveConfigByPrimaryKey();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "get live config");
        }

        return Response.ok(liveConfig);

    }


    //修改缓存相关配置
    @PostMapping("/cache/set")
    public Response setCacheConfig(@RequestBody JSONObject requestParams) {
        CacheConfig cacheConfig = new CacheConfig();

        cacheConfig.setCacheEnable(requestParams.getInteger("cacheEnable"));
        cacheConfig.setCacheTime(requestParams.getInteger("cacheTime"));
        cacheConfig.setCacheType(requestParams.getString("cacheType"));
        cacheConfig.setCacheSuffix(requestParams.getString("cacheSuffix"));

        try {
            baseConfigService.updateCacheConfigByPrimaryKey(cacheConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "set cacheConfig ");
        }
        return Response.ok();
    }

    //查询直播相关配置
    @PostMapping("/cache/get")
    public Response getCacheConfig(@RequestBody JSONObject requestParams) {
        CacheConfig cacheConfig;

        try {
            cacheConfig = baseConfigService.selectCacheConfigByPrimaryKey();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "get cache config");
        }

        return Response.ok(cacheConfig);

    }

    //修改收入分成配置
    @PostMapping("/share/set")
    public Response setShareConfig(@RequestBody JSONObject requestParams) {
        ShareConfig shareConfig = new ShareConfig();

        shareConfig.setAnchor(requestParams.getInteger("anchor"));
        shareConfig.setAnchorAgent(requestParams.getInteger("anchorAgent"));
        shareConfig.setChargeAgent(requestParams.getInteger("chargeAgent"));

        try {
            baseConfigService.updateShareConfigByPrimaryKey(shareConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "set shareConfig ");
        }
        return Response.ok();
    }

    //查询收入分成配置
    @PostMapping("/share/get")
    public Response getShareConfig(@RequestBody JSONObject requestParams) {
        ShareConfig shareConfig;

        try {
            shareConfig = baseConfigService.selectShareConfigByPrimaryKey();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "get share config");
        }

        return Response.ok(shareConfig);

    }


    @PostMapping("/roomType/allow/set")
    public Response setAllowRoomType(@RequestBody JSONObject requestParams) {
        String allowRoomType = requestParams.getString("allowRoomType");

        String allowRoomType_redisKey = "roomType:allow";

        redisUtils.string_set(allowRoomType_redisKey, allowRoomType);

        return Response.ok();

    }

    //魔方是否启用设置接口
    @PostMapping("/cube/config/set")
    public Response setCubeConfig(@Valid @RequestBody SetCubeConfigReq setCubeConfigReq) {

        int enableCube = setCubeConfigReq.getEnableCube();
        configService.setEnableCube(enableCube);
        return Response.ok();
    }

    //获取魔方是否启用设置接口
    @PostMapping("/cube/config/get")
    public Response getCubeConfig(@RequestBody RequestBodyBasic requestBodyBasic) {
        int enableCube = configService.getEnableCube();
        GetCubeConfigResponse getCubeConfigResponse = new GetCubeConfigResponse();
        getCubeConfigResponse.setEnableCube(enableCube);
        return Response.ok(getCubeConfigResponse);

    }

    //获取直播类型接口
    @PostMapping("/liveType/get")
    public Response getLiveType(@RequestBody RequestBodyBasic requestParamBasic) {
        return configService.getLiveType();
    }

    //设置直播类型接口
    @PostMapping("/liveType/set")
    public Response setLiveType(@RequestBody SetLiveTypeReq setLiveTypeReq) {
        String[] liveTypes = setLiveTypeReq.getLiveTypes();
        String liveType = liveTypes[0];
        for (int i = 1; i < liveTypes.length; i++) {
            liveType = liveType.concat(",").concat(liveTypes[i]);
        }
        configService.setLiveTypes(liveType);
        return Response.ok();
    }

}
